import { get, post } from '@/utils/request'

//获取列表
export const getNewList = (data = { page: 1, pagelimit: 5 }) => {
    return get('/index.php/index/news/newsList', { params: data })
        .then((res) => {
            if (res.code === 0) {
                return res;
            } else {
                throw new Error(`接口返回异常: ${res.msg}`);
            }
        });
};

//添加新闻
export const addNews = (data) => {
    return new Promise((res, rej) => {
        post('/index.php/index/news/addNews', data)
            .then(val => {
                res(val)
            }).catch(er => {
                rej(er)
            })
    })
}
//删除新闻
export const deletNews = (id) => {
    return post('/index.php/index/news/delNews', { id })  // 修正传参格式
        .then((res) => {
            if (res.code === 0) {
                return res;
            } else {
                throw new Error(`接口返回异常: ${res.msg}`);
            }
        });
};
//点赞新闻
export const likeNews = (data) => {
    return post('/index.php/index/news/addNum', data)  // 补全基础路径
        .then((res) => {
            if (res.code === 0) {
                return res;
            } else {
                throw new Error(`接口返回异常: ${res.msg}`);
            }
        });
};
//查看新闻详情页
export const getNewsDetail = (id) => {
    return post('/index.php/index/news/getNewsByid', { id })  // 补全路径并修正参数
        .then((res) => {
            if (res.code === 0) {
                return res;
            } else {
                throw new Error(`接口返回异常: ${res.msg}`);
            }
        });
};